|
Escape sequences are used in the programming languages C and C++, and also in many more languages (with some variations) like Java and C#. An escape sequence is a sequence of characters that does not represent itself when used inside a character or string literal, but is translated into another character or a sequence of characters that may be difficult or impossible to represent directly. In C, all escape sequences consist of two or more characters, the first of which is the backslash, \; the remaining characters determine the interpretation of the escape sequence. For example, \n is an escape sequence that denotes a newline character. The remainder of this article focuses on C; other programming languages are likely to have different syntax and semantics. == Motivation == Suppose we want to print out Hello, on one line, followed by world! on the next line. One could attempt to represent the string to be printed as a single literal as follows: #include int main() This is not valid in C, since a string literal may not span multiple logical source lines. This can be worked around by printing the newline character using its numerical value (0x0a in ASCII), #include int main() This instructs the program to print Hello,, followed by the character whose numerical value is 0x0a, followed by world!. While this will indeed work when the machine uses the ASCII encoding, it will not work on systems that use other encodings, that have a different numerical value for the newline character. It is also not a good solution because it still does not allow us to represent a newline character inside a literal, and instead takes advantage of the semantics of printf. In order to solve these problems and ensure maximum portability between systems, C interprets \n inside a character or string literal as a newline character, whatever that may be on the target system: #include int main() In this code, the escape sequence \n does not stand for a backslash followed by the letter n, because the backslash causes an "escape" from the normal way characters are interpreted by the compiler. After seeing the backslash, the compiler expects another character to complete the escape sequence, and then translates the escape sequence into the character it is intended to represent. Thus, "Hello,\nworld!" represents a string with an embedded newline, regardless of whether it is used inside printf or anywhere else. This raises the issue of how to represent an actual backslash inside a literal. This is done by using the escape sequence \\, as seen in the next section. 抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)』 ■ウィキペディアで「Escape sequences in C」の詳細全文を読む スポンサード リンク
|